package com.nsoft.thickclient.trackMove.vehicle;

import java.awt.GridLayout;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JPanel;

import com.nsoft.server.serviceImpl.FacadeFactoryImpl;
import com.nsoft.thickclient.components.VoPropertySearchField;
import com.nsoft.thickclient.components.VoPropertySearchFieldAction;
import com.nsoft.thickclient.components.VoPropertyTextArea;
import com.nsoft.thickclient.containers.NBoxPanel;
import com.nsoft.thickclient.containers.VOPropertyComponentPanel;
import com.nsoft.thickclient.controller.ScreenView;
import com.nsoft.thickclient.model.VoModel;
import com.nsoft.thickclient.trackMove.intr.party.ThinParty;
import com.nsoft.thickclient.trackMove.intr.vehicle.Vehicle;

public class VehicleView extends ScreenView<Vehicle> {

	/**
	 * private static final long serialVersionUID = 1L.
	 */
	private static final long serialVersionUID = 1L;

	private VoModel model;

	public VehicleView(Vehicle bean, boolean editMode) {
		model = new VoModel(bean);
		NBoxPanel mainPanel = new NBoxPanel(NBoxPanel.VERTICAL);
		mainPanel.addComponent(getDetailsPanel(editMode));
		mainPanel.addComponent(getOwnerDetailsPanel(editMode));
		mainPanel.addComponent(getRemarksPanel());
		this.add(mainPanel);
	}

	private JComponent getDetailsPanel(boolean editMode) {

		VOPropertyComponentPanel leftPanel = new VOPropertyComponentPanel();

		leftPanel.addVoTextField(model, "registrationNo", "Vehocle No",
				!editMode);
		leftPanel.addVoTextField(model, "make", "Manifacturer", true);
		leftPanel.addVoTextField(model, "insurence", "Insurence No", true);
		leftPanel.addVoNumericField(model, "tonnage", "Tonnage", false, true);

		VOPropertyComponentPanel rightPanel = new VOPropertyComponentPanel();

		rightPanel.addVoDateChooser(model, "registrationDate",
				"Registration Date", true);
		rightPanel.addVoTextField(model, "model", "Model", true);
		rightPanel.addVoDateChooser(model, "insurenceExpiryDate",
				"Insurence ExpiryD ate", true);
		rightPanel.addStrut(1);

		JPanel panel = new JPanel(new GridLayout(1, 2));
		panel.setBorder(BorderFactory.createTitledBorder("Vehicle details"));
		panel.add(leftPanel);
		panel.add(rightPanel);
		return panel;
	}

	private JComponent getOwnerDetailsPanel(boolean editMode) {

		VOPropertyComponentPanel leftPanel = new VOPropertyComponentPanel();

		leftPanel.addVoTextField(model, "owner", "Owner Name", true);
		leftPanel.addVoTextField(model, "lastDriverName", "Driver Name", true);

		VOPropertyComponentPanel rightPanel = new VOPropertyComponentPanel();
		VoPropertySearchField searchField = new VoPropertySearchField(model,
				"billingPartyId", "id", new VoPropertySearchFieldAction() {

					public List<?> getData(String filter) {
						return FacadeFactoryImpl.getPartyService().getAllThinParties(new ThinParty(filter,null));
					}

					public List<?> getDataByMappedavalue(Object value) {
						List<?>list= FacadeFactoryImpl.getPartyService().getAllThinParties(new ThinParty(null,((Long) value).intValue()));
						return list;
					}

				});
		/*if(editMode) {
			searchField.setValue(FacadeFactoryImpl.getPartyService().getAllThinParties(new ThinParty(null,((Long)model.getProperty("billingPartyId")).intValue())));
		}*/
		rightPanel.addComponent("Billing party name", searchField);
		rightPanel.addVoTextField(model, "lastDriverLicence",
				"Driver Licence No", true);
		JPanel panel = new JPanel(new GridLayout(1, 2));
		panel.setBorder(BorderFactory.createTitledBorder("Owner details"));
		panel.add(leftPanel);
		panel.add(rightPanel);
		return panel;
	}

	private JComponent getRemarksPanel() {

		VOPropertyComponentPanel panel = new VOPropertyComponentPanel();
		panel.setBorder(BorderFactory.createTitledBorder("Remarks"));
		VoPropertyTextArea textArea = new VoPropertyTextArea(model, "remarks");
		textArea.setRows(5);
		textArea.setColumns(60);
		panel.addComponent(null, textArea);
		return panel;

	}

	public VoModel getModel() {
		return model;
	}

	public void setModel(VoModel model) {
		this.model = model;
	}

	public Vehicle getWorkingBean() {
		return (Vehicle) model.getValueObject();
	}

	public void setWorkingBean(Vehicle bean) {
		model.setValueObject(bean);

	}

}
